
******MULTIPLE HYP TESTING ADJUSTED P-VALS (TABLE C.3 COL 4-5)********

//--> THE PROGRAM PERFORMS RANDOMIZATION INFERENCE BASED WY STEPDOWN PROCEDURE TO CALCUALTE P VALUES ADJUSTED FOR MULTIPLE HYPOTHESIS TESTING

adopath + "$main/DoFiles"																		

set seed 999

//----> DATA: SOCIAL AND CREDIT VARS
**********************************************************************************************************************************************************************

		use "$DIR2/fw_finvar", clear
		sort fw_code
		merge fw_code using "$DIR2/fw_socvar"
		*keep if _merge==3
		drop _merge
	
	
	
			egen cluster = group(fu_feb05)
	
			g treat_creditsm = treat_credit
			g x1 = treat_credit + treat_sm
			
			rename 	stdfin_index1 stdfin_index	
			
			tab region, gen(r)
			

//----> PROGRAM
**********************************************************************************************************************************************************************	



		# delimit ;
			local Y_main stdfin_index stdsocial_index;
				
			
			//--> generate variables to store actual and simulated t-stats/p-vals ;
			local counter = 1;
			local g 0;
			gen str20 varname = "";
			gen str20 group = "";
			gen float tstat = .;
			gen float act_pval = .;
			gen float tstatsim = .;
			gen float pvalsim = .;
			gen float pvals = .;

			//--> run the original regressions for all of the different outcomes tested and store the actual (observed) p-vals/t-stats;
			local c = 1;
			foreach group in treat_credit treat_sm treat_creditsm {;
			
				local ++g;
				foreach lhsvar in `Y_main' {;
					
		
					
					if `c'<3 {;
					
							
								quietly reg `lhsvar' treat_credit treat_sm r1 r2 r4, r;
								quietly replace tstat = abs(_b[`group']/_se[`group']) in `counter';
								quietly replace act_pval = 2*ttail(e(N),abs(tstat)) in `counter';
								quietly replace varname = "`lhsvar'" in `counter';
								quietly replace group = "`group'" in `counter';
								local `lhsvar'_`g'_ct_0 = 0;
					};
					
					else {;
					quietly reg `lhsvar' treat_creditsm x1 r1 r2 r4, r;	
					quietly replace tstat = abs(_b[`group']/_se[`group']) in `counter';
					quietly replace act_pval = 2*ttail(e(N),abs(tstat)) in `counter';
					quietly replace varname = "`lhsvar'" in `counter';
					quietly replace group = "`group'" in `counter';
					local `lhsvar'_`g'_ct_0 = 0;
					};
					

					local counter = `counter' + 1;
					};
				local c = `c' + 1;
				};

			//--> sort the p-vals by the actual (observed) p-vals (this will reorder some of the obs, but that shouldn't matter);
			gsort act_pval;
			local endvar = `counter' - 1;
			
			gen group_n = .;
			replace group_n=1 if group=="treat_credit";
			replace group_n=2 if group=="treat_sm";
			replace group_n=3 if group=="treat_creditsm";


			//--> generate the variable that will contain the simulated (placebo) treatments;
			gen byte sim_treat_credit = .;
			gen byte sim_treat_sm = .;
			gen byte sim_treat_creditsm = .;
			gen byte sim_x1 = .;		
			
			
			gen float simtreatment_uni = .;
			local count = 1;

			//--> run 10,000 iterations of the simulation, record results in p-val storage counters;
			while `count' <= 10000 {;
			//--> in this section we assign the placebo treatments and run regressions using the placebo treatments;
				
				
				
				preserve;
				bys cluster: keep if _n==1;
				
				quietly replace simtreatment_uni = uniform();
				quietly replace sim_treat_credit = inrange(simtreatment_uni,0,.33);
				quietly replace sim_treat_sm = inrange(simtreatment_uni,.33,.66);
				
				keep cluster simtreatment_uni sim_treat_credit sim_treat_sm;
				sort cluster;
				tempfile temp_cluster;
				save `temp_cluster', replace;
				restore;
				
				merge m:1 cluster using `temp_cluster', update replace;
				drop _merge;
				
				gsort act_pval;
				quietly replace sim_treat_creditsm = sim_treat_credit;
				quietly replace sim_x1 = sim_treat_credit + sim_treat_sm;
				
				
				
				quietly replace tstatsim = .;
				quietly replace pvalsim = .;
				foreach lhsvar of numlist 1/`endvar' {;
					local depvar = varname[`lhsvar'];
					local group = group[`lhsvar'];

					
						if `group'==treat_creditsm {;
						quietly reg `depvar' sim_treat_creditsm sim_x1 r1 r2 r4, r;	
						quietly replace tstatsim = abs(_b[sim_`group']/_se[sim_`group']) in `lhsvar';
						quietly replace pvalsim = 2*ttail(e(N),abs(tstatsim)) in `lhsvar';
						};
						
						else {;
						quietly reg `depvar' sim_treat_credit sim_treat_sm r1 r2 r4, r;	
						quietly replace tstatsim = abs(_b[sim_`group']/_se[sim_`group']) in `lhsvar';
						quietly replace pvalsim = 2*ttail(e(N),abs(tstatsim)) in `lhsvar';
						};
					};
			//--> in this section we perform the "step down" procedure that replaces simulated p-vals with the minimum of the set of simulated p-vals associated with outcomes that had actual p-vals greater than or equal to the one being replaced.  For each outcome, we keep count of how many times the ultimate simulated p-val is less than the actual observed p-val.;
				
				local countdown = `endvar';
				while `countdown' >= 1 {;
					quietly replace pvalsim = min(pvalsim,pvalsim[_n+1]) in `countdown';
					local depvar = varname[`countdown'];
					local group_n = group_n[`countdown'];
					if pvalsim[`countdown'] <= act_pval[`countdown'] {;
						local `depvar'_`group_n'_ct_0 = ``depvar'_`group_n'_ct_0' + 1;
						};
					local countdown = `countdown' - 1;
					};
					
					
				local count = `count' + 1;
				};

			//--> perform the final adjustment that ensures that the ordering to adjusted p-vals is the same as the original ordering of actual p-vals.;
			foreach lhsvar of numlist 1/`endvar' {;
				local depvar = varname[`lhsvar'];
				local group_n = group_n[`lhsvar'];
				quietly replace pvals = max(round(``depvar'_`group_n'_ct_0'/10000,.001), pvals[`lhsvar'-1]) in `lhsvar';
				};

			sort group varname;
			keep if varname!="";
			keep varname group act_pval pvals;

			tempfile f;
			save "`f'";	
		

		use "`f'", clear;
		set obs `=_N+1';

	#delimit cr	


//----> OUTPUT: ONLINE APPENDIX TABLE C.3; COLS 4-5
**********************************************************************************************************************************************************************	


export excel "$output/tableC_3 (col 4-5 pavlues)", replace firstrow(var)

